package org.opu.pc.sg;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import org.opu.pc.util.DateUtil;
import org.apache.commons.io.IOUtils;
import org.opu.pc.ui.util.Logger;
import static org.opu.pc.sg.StudentStatementGenerator.HeaderType.*;

/**
 *
 * @author Yuriy Dunko
 */
public class StatementGenerationServicesImpl implements StatementGenerationServices {

    private Logger logger = Logger.create(StatementGenerationServicesImpl.class);

    private AbstractStatementGenerator departmentTemplate;
    private AbstractStatementGenerator studentsTemplate;
    private AbstractStatementGenerator professorTemplate;

    public StatementGenerationServicesImpl(
            AbstractStatementGenerator departmentTemplate,
            AbstractStatementGenerator studentsTemplate,
            AbstractStatementGenerator professorTemplate) {
        this.departmentTemplate = departmentTemplate;
        this.studentsTemplate = studentsTemplate;
        this.professorTemplate = professorTemplate;
    }
    
    @Override
    public void departmentPublicationsStatement(Date begin, Date end,
            OutputStream output) throws IOException {
        basePrepare(begin, end, "studentScienceStatement", "st1", departmentTemplate);
        generate(departmentTemplate, output);
    }

    @Override
    public void studentScienceStatement(Date begin, Date end,
            OutputStream output) throws IOException {
        basePrepare(begin, end, "studentScienceStatement", "st2", studentsTemplate);
        studentsTemplate.addVariable(StudentStatementGenerator.HEADER_TYPE, STUDENT);
        generate(studentsTemplate, output);
    }

    @Override
    public void professorScienceStatement(Date begin, Date end,
            Integer professorId, OutputStream output) throws IOException {
        basePrepare(begin, end, "professorScienceStatement", "st2", professorTemplate);
        professorTemplate.addVariable(StudentStatementGenerator.HEADER_TYPE, PROFESSOR);
        professorTemplate.addVariable(ProfessorStatementGenerator.PROFESSOR_ID, professorId);
        generate(professorTemplate, output);
    }

    private void basePrepare(Date begin, Date end, String methodName,
            String propName, AbstractStatementGenerator template) throws IOException{

        int y1 = DateUtil.getYear(begin);
        int y2 = DateUtil.getYear(end);

        logger.info(methodName + ": start prepare statement: " + y1 + " - " + y2);

        template.resetSGProperties("/org/opu/pc/sg/" + propName + "_properties.properties");
        template.setSGProperty(DepartmentStatementGenerator.BEGIN_YEAR_VAR, String.valueOf(y1));
        template.setSGProperty(DepartmentStatementGenerator.END_YEAR_VAR, y1 != y2 ? "- " + y2 : "");

        template.resetVariables();
        template.addVariable(DepartmentStatementGenerator.BEGIN_YEAR_VAR, begin);
        template.addVariable(DepartmentStatementGenerator.END_YEAR_VAR, end);
    }

    private void generate(AbstractStatementGenerator template,
            OutputStream output) throws IOException {
        try {
            IOUtils.write(template.generateDocument(), output);
        } finally {
            IOUtils.closeQuietly(output);
        }
    }
}
